package a;

import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.schmizz.sshj.SSHClient;
import net.schmizz.sshj.sftp.FileAttributes;
import net.schmizz.sshj.sftp.RemoteResourceInfo;
import net.schmizz.sshj.sftp.SFTPClient;
import net.schmizz.sshj.transport.verification.PromiscuousVerifier;

import java.io.IOException;
import java.util.List;

/**
 * @description:
 * @author: jinzewang
 * @project_name: farmpost
 * @date: 2022/12/15 10:34
 */
@Slf4j
@Data
@NoArgsConstructor
public class SFTPSSHJUtils {
    private String remoteHost;
    private String username;
    private String password;
    private String remotePath;
    private String localPath;

    public SFTPSSHJUtils(String remoteHost, String username, String password, String remotePath, String localPath) {
        this.remoteHost = remoteHost;
        this.username = username;
        this.password = password;
        this.remotePath = remotePath;
        this.localPath = localPath;
    }

    private static SFTPClient openSession(SSHClient ssh, String hostName, String username, String password) {
        SFTPClient sftpClient;
        try {
            ssh.addHostKeyVerifier(new PromiscuousVerifier());
            ssh.connect(hostName);
            ssh.authPassword(username, password);
            sftpClient = ssh.newSFTPClient();
            return sftpClient;
        } catch (Exception e) {
            log.error("sftp connect error......, msg is :[{}]", e.toString());
            return null;
        }
    }

    public static void downLoadFileBySsh(String hostName, String username, String password, String remoteFilePath, String localFilePath) {
        SSHClient ssh = new SSHClient();
        SFTPClient sftpClient = openSession(ssh, hostName, username, password);
        try {
            //ssh.loadKnownHosts(); to skip host verification
            assert sftpClient != null;
            sftpClient.get(remoteFilePath, localFilePath);
            //create a folder
//            sftpClient.mkdir("/opt/app/testFolder");
            //sftpClient.mkdirs("");创建多级文件夹
            //sftpClient.rmdir("");重命名文件夹
//            sftpClient.ls(""); //列出当前目录
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        } finally {
            if (null != sftpClient) {
                try {
                    sftpClient.close();
                } catch (IOException e) {
                    log.error("SFTP downLoadFileBySsh sftpClient disconnect error...msg is {{}]", e.toString());
                }
                try {
                    ssh.disconnect();
                } catch (IOException e) {
                    log.error("SFTP downLoadFileBySsh ssh disconnect error...msg is {{}]", e.toString());
                }
            }
        }
    }

    public static void uploadFileBySsh(String hostName, String username, String password, String remotePathOrFile, String localPathOrFIle) {
        SSHClient ssh = new SSHClient();
        SFTPClient sftpClient = openSession(ssh, hostName, username, password);
        try {

            assert sftpClient != null;
            sftpClient.put(localPathOrFIle, remotePathOrFile);
        } catch (IOException e) {
            log.error("SFTP uploadFileBySsh put error...msg is [{}]", e.toString());
        }finally {
            try {
                ssh.disconnect();
            } catch (IOException e) {
                log.error("SFTP uploadFileBySsh SSHClient disconnect error...msg is [{}]", e.toString());
            }
            try {
                sftpClient.close();
            } catch (IOException e) {
                log.error("SFTP uploadFileBySsh sftpClient disconnect error...msg is [{}]", e.toString());
            }
        }
    }
}
